package com.drguo.higouguo.web;

import java.io.File;
import java.io.IOException;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Required;

import com.drguo.higouguo.entity.Category;
import com.drguo.higouguo.entity.Goods;
import com.drguo.higouguo.entity.PageBean;
import com.drguo.higouguo.service.CategoryService;
import com.drguo.higouguo.service.GoodsService;
import com.drguo.higouguo.utils.GuoUtil;

public class GoodsAction {
	
	private CategoryService categoryService;
	private List<Category> categories;
	private String goodsNo;
	private Goods goods;
	private GoodsService goodsService;
	private File thumbnail;
	private String thumbnailFileName;

	// 增加商品
	public String add() {
		// 商品货号
		goodsNo = "GUO" + GuoUtil.getGoodsNo();
		// //获得商品类别表的类别（添加商品时所属分类下拉列表）
		categories = categoryService.getCategories();
		return "add";
	}
	public String addOpr() {
		// 商品缩略图存放路径
		String dir = ServletActionContext.getServletContext().getRealPath(
				"/goodsimages");
		String suffix = "";
		String fileName = GuoUtil.getId();
		if (thumbnail != null) {
			suffix = thumbnailFileName.substring(thumbnailFileName.lastIndexOf("."));
			//存放路径，保存的文件名=（UUID+原文件名），为了防止重复！
			File saveFile = new File(new File(dir), fileName + suffix);
			if (!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdir();
			}
			try {
				FileUtils.copyFile(thumbnail, saveFile);
			} catch (IOException e) {

				e.printStackTrace();
			}
			goods.setThumbnail("goodsimages/" + fileName + suffix);
		}
		goodsService.addGoods(goods);

		return "list";
	}
	
	//商品列表
	private List<Goods> goodses;
	public String list(){
		goodses=goodsService.getGoods(goods);//获得商品表的商品
		categories = categoryService.getCategories();//获得商品类别表的类别
		return "list";
	}
	
	//分页
	private PageBean<Goods> pageBean;
	private int page;
	public String listByPage(){
		categories = categoryService.getCategories();//获得商品类别表的类别
		if(page==0){
			page=1;
		}
		pageBean=goodsService.getGoodsByPage(page, goods);
		return "list";
	}
	
	//商品修改页面
	public String update(){
		categories = categoryService.getCategories();//获得商品类别表的类别
		goods = goodsService.getGoodsById(goods.getId());
		return "update";
	}
	
	//商品修改
	public String updateOpr(){
		if (thumbnail != null) {
			String dir = ServletActionContext.getServletContext().getRealPath("/");
			String suffix = thumbnailFileName.substring(thumbnailFileName.lastIndexOf("."));
			String fileName = GuoUtil.getId();
			//存放路径，保存的文件名=（UUID+原文件名），为了防止重复！
			File saveFile = new File(new File(dir+"/goodsimages"), fileName + suffix);
			if (!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdir();
			}
			try {
				FileUtils.copyFile(thumbnail, saveFile);
			} catch (IOException e) {

				e.printStackTrace();
			}
			//修改缩略图之后把原来的删掉
			new File(dir+"/"+goods.getThumbnail()).delete();
			
			goods.setThumbnail("goodsimages/" + fileName + suffix);
		}
		goodsService.updateGoods(goods);
		return "updatesuc";
	}
	
	//商品删除
	public String del(){
		goodsService.delGoods(goods.getId());
		return "delsuc";
	}
	
	//根据类别获取商品列表
	public String listByCate(){
		goodses=goodsService.getGoodsByCateId(goods.getCategoryId());
		return "bycate";
	}
	
	//单个商品查看页面
	public String view(){
		goods = goodsService.getGoodsById(goods.getId());
		return "view";
	}
	
	//为了购物车
	private String goodsIds;
	private String result;
	public String getGoodsesByIds(){
		String[] ids = goodsIds.split(",");
		goodses=goodsService.getGoodsesByIds(ids);
		//产生json时不考虑下列值，减小json大小
		JsonConfig c = new JsonConfig();
		c.setExcludes(new String[]{"category","goodsNo","categoryId",
				"price1","stock","description"});
		
		JSONArray a = JSONArray.fromObject(goodses,c);
		result = a.toString();
		return "getgoodsesbyids";
	}
	
	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public String getGoodsNo() {
		return goodsNo;
	}

	public void setGoodsNo(String goodsNo) {
		this.goodsNo = goodsNo;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public GoodsService getGoodsService() {
		return goodsService;
	}
	@Required
	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public File getThumbnail() {
		return thumbnail;
	}

	public void setThumbnail(File thumbnail) {
		this.thumbnail = thumbnail;
	}

	public String getThumbnailFileName() {
		return thumbnailFileName;
	}

	public void setThumbnailFileName(String thumbnailFileName) {
		this.thumbnailFileName = thumbnailFileName;
	}
	public List<Goods> getGoodses() {
		return goodses;
	}
	public void setGoodses(List<Goods> goodses) {
		this.goodses = goodses;
	}
	public PageBean<Goods> getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean<Goods> pageBean) {
		this.pageBean = pageBean;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getGoodsIds() {
		return goodsIds;
	}
	public void setGoodsIds(String goodsIds) {
		this.goodsIds = goodsIds;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}

}
